<?php

define('FONT_SIZE', 8);

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class ReportpdfController extends modules_default_controllers_ControllerBase
{
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
	public function indexAction()
    {

        $oRequest         = $this->getRequest();

        $archive          = $oRequest->getParam('archive');    
        if ('Current month' == $archive)
        {
            $archive = false; 
        }
            
        if (!$archive)
        {
            $startDate  = date('Y-m-1');
            $endDate    = date('Y-m-t');
        }  
        else
        {
            $startDate    = $archive . '-1';
            $endDate      = date('Y-m-t', strtotime($startDate));
        }
        
        
        list($year, $month, $day) = explode('-', $startDate);
        
        $aIdentity    = Zend_Auth::getInstance()->getIdentity();
        $idUser       = $aIdentity['User_id'];
        $reportPeriod = models_PeriodMapper::findByIdUser($idUser, $startDate);
        $user         = models_UserMapper::findById($idUser);
        $allProject   = models_ProjectMapper::findByIdUser($idUser, $startDate, $endDate);
        $allReport    = models_ReportMapper::findByIdUser($idUser, $startDate, $endDate);
        
        $retReport    = array();
        foreach ($allReport as $report)
        {
            $day = date('j', strtotime($report['date']));
            $retReport[$day][$report['id_project']] = $report['hour'];
        }

        $archiveDate    = models_ReportMapper::getAllArchiveDate();
        $attach         = models_PrinttimesheetMapper::findByIdUserAndDate($idUser, $startDate);
        
            
        if(isset($attach[0])) 
        {   
            $this->view->attach   = $attach[0]; 
        }   
        $this->view->archiveDate  = $archiveDate;
        $this->view->archive      = $archive;
        $this->view->endDay       = date('t', strtotime($endDate));
        $this->view->user         = $user;
        $this->view->period       = date('F Y', strtotime($endDate));
        $this->view->allProject   = $allProject;
        $this->view->allReport    = $retReport;        
        $this->view->month        = $month;
        $this->view->year         = $year;
        if (isset($reportPeriod[0]))
        {
            $this->view->reportPeriod       = $reportPeriod[0];
        }
    	
//--------------------------------------------
//      array with user data, for print in header 
        
        $aUser = array( 'Name'      => $user->name,
                        'Afkorting' => $user->afk,
                        'Periode'   => date('F Y', strtotime($endDate)),
                        );
//--------------------------------------------
//      day list in tableheader  
        
        $arrayCouner = 0;
        
        $header[1] = '';
        $headerColor[1] = '';
        for($i = 1; $i <= $this->view->endDay; $i++)
        {
            $headerColor[] = self::getDayColor($i, $this->view->year, $this->view->month);
            $header[] =  date('D', strtotime($this->view->year . '-' .  $this->view->month . '-' . $i)); 
        }
        $header[] = '';
        $aHeader[$arrayCouner]['data']  = $header;
        $aHeader[$arrayCouner]['color'] = $headerColor;
        
        unset($header);
        unset($headerColor);
        
        $arrayCouner++;
//--------------------------------------------   
//      day data in tableheader

        $header[1] = '';
        $headerColor[1] = '';
        
        for($i = 1; $i <= $this->view->endDay; $i++)
        {
            $headerColor[] = self::getDayColor($i, $this->view->year, $this->view->month);
            $header[] =   $i; 
        }        
        $header[] = 'Totaal';
        $aHeader[$arrayCouner]['data']  = $header;
        $aHeader[$arrayCouner]['color'] = $headerColor;

        $arrayCouner = 0;
        
        foreach($this->view->allProject as $project)
        {
            unset($headerColor);
            $headerColor[1] = '';
        	
            $tData = array($project['project_num'], $project['name']);
        	 
        	   for($i = 1; $i <= $this->view->endDay; $i++)
        	   {
        	   	
//	        	   if (($i >= $startDay && $i <= $endDay) || (1 == $project['base'])) 
//	               {
	                   $val = '';
	                   if (isset($retReport[$i]))
	                       if (isset($retReport[$i][$project['id']]))
	                           $val = $retReport[$i][$project['id']] ;
//	               }
                        array_push($tData, $val);
                        if (isset($total))
	                        $total += $val;
 	                    else
	                        $total = $val;
                        
                        $headerColor[] = self::getDayColor($i, $this->view->year, $this->view->month);
        	   }
        	 array_push($tData, $total);
        	 unset($total);
        	 $data[$arrayCouner]['data']  = $tData ;
             $data[$arrayCouner]['color'] = $headerColor ;
        	 $arrayCouner++ ;
        }    
        
//--------------------------------------------       

        $normal_hour = $over_hour = $total_hour = array('', '');
        
        foreach($data as$aData)
        {
        	for($i = 2; $i < sizeof($aData['data']); $i++)
        	{
        		$hh = isset($aData['data'][$i]) ? $aData['data'][$i] : 0;
        		
        		if(isset($total_hour[$i]))
        		{
        		    $total_hour[$i]   +=  $hh;
        		}
        		else
        		{
        			$total_hour[$i] = $hh;
        		}
        	} 
        }
        
        foreach($total_hour as $day => $hour)
        {
            $normal_hour[$day] = $total_hour[$day] > 8 ? 8 : $total_hour[$day] ;        	
        	$over_hour[$day]   = $total_hour[$day] > 8 ? $total_hour[$day] - 8 : '';
        }

        
        $tData = array('', '');    
        for($i = 1; ($i <= $this->view->endDay + 1); $i++)
        {
        	$tData[] = '';
        }
             $data[$arrayCouner]['data']  = $tData ;
             $data[$arrayCouner]['color'] = $headerColor ;
             $arrayCouner++ ;
             $data[$arrayCouner]['data']  = $normal_hour ;
             $data[$arrayCouner]['color'] = $headerColor ;
             $arrayCouner++ ;
             $data[$arrayCouner]['data']  = $over_hour ;
             $data[$arrayCouner]['color'] = $headerColor ;
             $arrayCouner++ ;             
             $data[$arrayCouner]['data']  = $total_hour ;
             $data[$arrayCouner]['color'] = $headerColor ;
             $arrayCouner++ ;  

//--------------------------------------------
             
        $width     = array(13, 30);
        $width[($this->view->endDay +2 )] = 15;
        
		$pdf = new VLFN_Pdf('l');

		$pdf->SetMargins(4,2,4); 
		$pdf->AddPage();
		$pdf->SetFont('Arial', '', FONT_SIZE);
		
		$pdf->Table($data, $aHeader, $width, $aUser);
		
		$pdf->Output();
    }
    
    
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    public static function getDayColor($i, $year, $month)
    {   
       if (6 == date('w', strtotime($year . '-' .  $month . '-' . $i)) || 
             0 == date('w', strtotime($year . '-' .  $month . '-' . $i))) 
         {   
             $dayColor =  'pink';
         }    
         else   
         {   
             $dayColor =  '';
         }   
            
       return $dayColor;
    }        
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
}